package com.google.code.ptrends.catparsers;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.commons.lang.StringUtils;

import com.google.code.ptrends.catparsers.utils.RegexHelper;
import com.google.code.ptrends.common.entities.Category;
import com.google.code.ptrends.common.entities.Property;
import com.google.code.ptrends.common.entities.Session;
import com.google.code.ptrends.entities.ETLItem;
import com.google.code.ptrends.entities.ParametersStrings;
import com.google.code.ptrends.excel.ExcelReader;
import com.google.code.ptrends.excel.entities.Worksheet;

public class HDDCategoryParser extends AbstractCategoryParser {

	private List<Matcher> matcher;

	public HDDCategoryParser(final ExcelReader reader,
			final Worksheet worksheet, final Session session) {
		super(reader, worksheet, session);
		matcher = new ArrayList<Matcher>();
		matcher.add(Pattern.compile(RegexHelper.getRegex("hdd1")).matcher(""));
		matcher.add(Pattern.compile(RegexHelper.getRegex("hdd2")).matcher(""));
	}

	@Override
	protected ETLItem createItem(final String description, final String priceUSD) {

		ETLItem item = null;

		if (StringUtils.isNotBlank(description)
				&& StringUtils.isNotBlank(priceUSD)) {
			for (Matcher m : matcher) {
				m.reset(description);
				if (m.find()) {
					item = buildItem(Category.HDD, m.group(4), m.group(5),
							new Float(priceUSD), new Date());

					final java.util.List<Property> property = new ArrayList<Property>();

					// form factor
					if (StringUtils.isNotBlank(m.group(1))) {
						property.add(buildProperty(
								ParametersStrings.FORM_FACTOR, m.group(1)));
					}
					// interface
					if (StringUtils.isNotBlank(m.group(2))) {
						property.add(buildProperty(ParametersStrings.INTERFACE,
								m.group(2)));
					}
					// Capacity
					if (StringUtils.isNotBlank(m.group(3))) {
						property.add(buildProperty(ParametersStrings.CAPACITY,
								m.group(3)));
					}
					// rotation speed
					if (StringUtils.isNotBlank(m.group(7))) {
						property.add(buildProperty(
								ParametersStrings.ROTATION_SPEED, m.group(7)));
					}

					// Cache size
					if (StringUtils.isNotBlank(m.group(8))) {
						property.add(buildProperty(
								ParametersStrings.CACHE_SIZE, m.group(8)));
					}

					item.setParameters(property);
				}
			}
		}
		return item;
	}
}